package com.bw.fzj;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class HbaseTest {

    public static void main(String[] args) throws IOException {
        //导入数据
        //putDataTbale("good:one");
        //查询所有
        //scanTbale("good:one");
        //月销为0删除
        //ValueFilter("good:one");
        //查看表中的包含polo衫的数据；
        //scanone("good:one");
        //del("good:one");
        find("good:one");
    }



    public static Configuration conf;
    static {
        conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.200.100");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }

    public static boolean isExc(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        return admin.tableExists(TableName.valueOf(tableName));
    }

    public static void createTbale(String tableName,String... cf) throws IOException {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for (String s : cf) {
            HBaseAdmin admin = new HBaseAdmin(conf);
            admin.createTable(descriptor);
            admin.close();
        }

    }
    public static void putDataTbale(String tableName) throws IOException {
        HTable hTable = new HTable(conf, tableName);

        LinkedList<Put> linkedList = new LinkedList<Put>();
        FileReader fileReader = new FileReader(new File("./data/one.txt"));
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        //空字符串用于保存读取的每一行数据
        String line = "";
        while((line=bufferedReader.readLine())!=null){
            String[] split = line.split(",");
            Put put = new Put(split[0].getBytes());
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("addr"), Bytes.toBytes(String.valueOf(split[1])));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(String.valueOf(split[2])));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("dname"), Bytes.toBytes(String.valueOf(split[3])));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("num"), Bytes.toBytes(String.valueOf(split[4])));
            linkedList.add(put);
        }
        hTable.put(linkedList);
        System.out.println("数据保存完成");

    }

    //查询所有
    public static void scanTbale(String tableName) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Scan scan = new Scan();
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+"\t"+
                        Bytes.toString(CellUtil.cloneFamily(cell))+"\t"+
                        Bytes.toString(CellUtil.cloneQualifier(cell))+ "\t"+
                        Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    //）使用hbase删除月销售为0的信息,然后查看查看表中是否存在月销为0的数据；
    public static void ValueFilter(String tabelName) throws IOException {
        HTable hTable = new HTable(conf, tabelName);
        Scan scan = new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("0"));
        Scan scan1 = scan.setFilter(filter);
        ResultScanner scanner = hTable.getScanner(scan1);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                String s = new String(CellUtil.cloneValue(cell));
                Delete delete = new Delete(s.getBytes());
                hTable.delete(delete);
                System.out.println("删除成功");
            }
        }
    }

    //查看表中的包含polo衫的数据；
    public static void scanone(String tabelName) throws IOException {
        HTable hTable = new HTable(conf, tabelName);
        Scan scan = new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("polo衫"));
        Scan scan1 = scan.setFilter(filter);
        ResultScanner scanner = hTable.getScanner(scan1);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+"\t"+
                        Bytes.toString(CellUtil.cloneValue(cell))+"\t");
                hTable.close();
            }
        }
    }

    //查询月销100以上的旗舰店名称
    public static void test2(String tabelName) throws IOException {
        HTable hTable = new HTable(conf, tabelName);
        Scan scan = new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator("xiaoliang".getBytes()));
        Scan scan1 = scan.setFilter(filter);
        ResultScanner scanner = hTable.getScanner(scan1);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                System.out.println("CF:" + new String(CellUtil.cloneFamily(cell)) +
                        ",CN:" + new String(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + new String(CellUtil.cloneValue(cell)));
                hTable.close();
            }
        }
    }

    //删除
    public static void del(String tabelName) throws IOException {
        HTable hTable = new HTable(conf, tabelName);
        Scan scan = new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("uenshadi官方旗舰店"));
        Scan scan1 = scan.setFilter(filter);
        ResultScanner scanner = hTable.getScanner(scan1);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                String s = new String(CellUtil.cloneValue(cell));
                Delete delete = new Delete(s.getBytes());
                hTable.delete(delete);
                System.out.println("删除成功！！！");
                hTable.close();
            }
        }
    }

    //查看艾衫印象旗舰店的商品信息（
    public static void find(String tabelName) throws IOException {
        HTable hTable = new HTable(conf, tabelName);
        Scan scan = new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("肯德基会员官方旗舰店"));
        Scan scan1 = scan.setFilter(filter);
        ResultScanner scanner = hTable.getScanner(scan1);
        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                System.out.println("CF:" + new String(CellUtil.cloneFamily(cell)) +
                        ",CN:" + new String(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + new String(CellUtil.cloneValue(cell)));
            }
        }
    }


}
